<?php
// The source code packaged with this file is Free Software, Copyright (C) 2005 by
// Ricardo Galli <gallir at uib dot es>.
// It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
// You can get copies of the licenses here:
// 		http://www.affero.org/oagpl.html
// AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".

if(!defined('mnminclude')){header('Location: ../404error.php');}

class Friend {
	var $friend = "";


	function remove($friend)
	{
		global $db,$current_user;
		if (!is_numeric($friend)) die();

		$sql = "Delete from " . table_friends . " where (friend_from = " . $current_user->user_id . " and friend_to = " . $friend. ") or (friend_from = " . $friend. " and friend_to = " . $current_user->user_id . " )";
		
		$sql2 = "Delete from pligg_namigos where (f1 = " . $current_user->user_id . " and f2 = " . $friend. ") or (f1 = " . $friend. " and f2 = " . $current_user->user_id . " )";
		
		//echo $sql;
		$db->query($sql);
		$db->query($sql2);

		$friend_status = $this->get_friend_status($friend);
		if ($friend_status){die("there was an error");}
		
	}

        function removeRecomend($friend)
	{
		global $db,$current_user;
		if (!is_numeric($friend)) die();

		//$sql = "Delete from " . table_friends . " where (friend_from = " . $current_user->user_id . " and friend_to = " . $friend. ") or (friend_from = " . $friend. " and friend_to = " . $current_user->user_id . " )";

		$sql2 = "Delete from pligg_recomendados where (id = ".$friend.")";

		//echo $sql;
		//$db->query($sql);
		$db->query($sql2);

		//$friend_status = $this->get_friend_status($friend);
		//if ($friend_status){die("there was an error");}

	}

	
	function removeBuscando($idb)
	{
		global $db,$current_user;
		

		$sql = "Delete from pligg_buscando where id = " . $idb;
		echo $idb;
		$db->query($sql);

		//$friend_status = $this->get_friend_status($friend);
		if ($friend_status){die("there was an error");}
		
	}
	
	
	
	function get_recomend_list($friendN)
	{
		// returns an array of people who have added you as a friend
		global $db, $current_user;
		
		//$friends2 = $db->get_results("SELECT pligg_users.user_login, pligg_users.user_avatar_source, pligg_users.user_email, pligg_users.user_id FROM pligg_recomendados INNER JOIN pligg_users ON pligg_recomendados.recomendadop = pligg_users.user_login WHERE (pligg_recomendados.recomendado= '".$friend."');",ARRAY_A);
		//echo $friendN;
		
		
		$friends = $db->get_results("SELECT " . table_users . ".user_login, " . table_users . ".user_names, " . table_users . ".user_avatar_source, " . table_users . ".user_email, " . table_users . ".user_id, pligg_recomendados.id FROM pligg_recomendados INNER JOIN " . table_users . " ON pligg_recomendados.recomendadop = " . table_users . ".user_login WHERE (pligg_recomendados.recomendado= '" .$friendN. "');",ARRAY_A);
	//echo " kkkkkkkkkkkkkkkkkkkkkkk ";
		return $friends;
	}
	
	
	function recomend($friend)
	{
		global $db,$current_user;
		if (!is_numeric($friend)) die();

		$db->query("INSERT IGNORE INTO pligg_namigos (recomendadop, recomendado) values (" . $current_user->user_id.", ".$friend.");");
		//echo $sql;
		

		//$friend_status = $this->get_friend_status($friend);
		//if ($friend_status){die("there was an error");}
		
	}
	
	function confirm($friend,$friendName, $friendMail)
	{
		global $db,$current_user;
		if (!is_numeric($friend)) die();

		$sql = "UPDATE " . table_friends . "  SET status = 2  WHERE (friend_from = " . $current_user->user_id . " AND friend_to = " . $friend. ") OR (friend_from = " . $friend. " AND friend_to = " . $current_user->user_id . " )";
		//echo $sql;
		$db->query($sql);
		
		$db->query("INSERT IGNORE INTO pligg_namigos (f1, f2) values (" . $current_user->user_id.", ".$friend.");");

		//echo $friendName;
$email_toC = $friendMail;

$email_subjectC = "Hola, han aceptado tu solicitud de contacto en BisClick";
$email_messageC = "Hola ".$friendName.", tu solicitud de contacto en bisclick ha sido aceptada por "."<br /> <br /> Bisclick ";

		$email_headersC = "From: contactos@bisclick.com \r\nReply-To:  contactos@bisclick.com \r\nContent-Type: text/html; charset=UTF-8";
        @mail($email_toC, $email_subjectC, $email_messageC, $email_headersC);



		//$friend_status = $this->get_friend_status($friend);
		//if ($friend_status){die("there was an error");}
		
	}
         function addS($friend1,$friend2)
	{
		global $db, $current_user;
	/*	if (!is_numeric($friend)) die();

		if ($current_user->user_id == 0) {
        echo "<span class='success' style='border:solid1px#269900;padding:2px2px2px2px'>Please <a href=" .my_base_url.my_pligg_base. "/login.php?return=/user.php?login=god&amp;view=addfriend>login</a></span><br/>";
        return;
        }*/

		/*$friend_status = $this->get_friend_status($friend);
		if (!$friend_status){*/
			//echo "INSERT INTO " . table_friends . " (friend_from, friend_to) values ($current_user->user_id, $friend);";
		$db->query("INSERT IGNORE INTO " . table_friends . " (friend_from, friend_to, status) values (" . $friend2.", ".$friend1.",3".");");


		//	$friend_status = $this->get_friend_status($friend);
		//	if (!$friend_status){die("there was an error");}

		//}
	}
	
	function add($friend)
	{
		global $db, $current_user;
		if (!is_numeric($friend)) die();
		
		if ($current_user->user_id == 0) {
        echo "<span class='success' style='border:solid1px#269900;padding:2px2px2px2px'>Please <a href=" .my_base_url.my_pligg_base. "/login.php?return=/user.php?login=god&amp;view=addfriend>login</a></span><br/>";
        return;
        }
		
		$friend_status = $this->get_friend_status($friend);
		if (!$friend_status){
                    //$friend_status = $this->get_friend_status($friend);
			//echo "INSERT INTO " . table_friends . " (friend_from, friend_to) values ($current_user->user_id, $friend);";
			$db->query("INSERT IGNORE INTO " . table_friends . " (friend_from, friend_to, status) values (" . $current_user->user_id.", ".$friend.",1".");");
			

			$friend_status = $this->get_friend_status($friend);
			if (!$friend_status){die("there was an error");}

		}
	}
	function get_friend_list_queued($user_id){
			// returns an array of people you've added as a friend
		global $db, $current_user;
		//echo "SELECT " . table_users . ".user_login FROM " . table_friends . " INNER JOIN " . table_users . " ON friends.friend_to = " . table_users . ".user_id WHERE (((friends.friend_from)=$current_user->user_id));";
		$friends = $db->get_results("SELECT " . table_users . ".user_login, " . table_users . ".user_names, " . table_users . ".user_avatar_source, " . table_users . ".user_email, " . table_users . ".user_id, " . table_friends. ".status  FROM " . table_friends . " LEFT JOIN " . table_users . " ON (" . table_friends . ".friend_from = " . table_users . ".user_id )  WHERE ((" . table_friends . ".friend_to)= " . $current_user->user_id . " AND (".table_friends.".status = 1 OR ".table_friends.".status = 3));",ARRAY_A);
		return $friends;
		

		
		
	}
	function get_search($buscando)
	{
	global $db, $current_user;
	$friends = $db->get_results("SELECT * FROM pligg_users WHERE user_login LIKE '%".$buscando."%' OR user_names LIKE '%".$buscando."%'  OR user_descripcion LIKE '%".$buscando."%' OR user_experiencia LIKE '%".$buscando."%' OR user_ubicacion LIKE '%".$buscando."%' OR user_pservicio LIKE '%".$buscando."%' OR user_reconocimiento LIKE '%".$buscando."%' OR user_clientes LIKE '%".$buscando."%' ",ARRAY_A);
//	echo $buscando;
	return $friends;
	}
	function get_friend_list($user_id)
	{	
		// returns an array of people you've added as a friend
		global $db, $current_user;
		//echo "SELECT " . table_users . ".user_login FROM " . table_friends . " INNER JOIN " . table_users . " ON friends.friend_to = " . table_users . ".user_id WHERE (((friends.friend_from)=$current_user->user_id));";
	
	// $friends = $db->get_results("SELECT " . table_users . ".user_login AS user_log, " . table_users . ".user_avatar_source AS user_av, " . table_users . ".user_email AS user_m, " . table_users . ".user_id AS user_d FROM " . table_friends . " AS t_friends LEFT JOIN " . table_users . " AS table_1 ON " . table_friends . ".friend_to = " . table_users . ".user_id  WHERE (((" . table_friends . ".friend_from)= " . $user_id . ")) LEFT JOIN " . table_users . "  AS table_2 ON " . table_friends . ".friend_from = " . table_users . ".user_id  WHERE (((" . table_friends . ".friend_to)= " . $user_id . "));",ARRAY_A);
	
	$friends = $db->get_results("SELECT " . table_users . ".user_login, " . table_users . ".user_names, " . table_users . ".user_avatar_source, " . table_users . ".user_email, " . table_users . ".user_id FROM " . table_friends . " LEFT JOIN " . table_users . " ON (" . table_friends . ".friend_to = " . table_users . ".user_id )  WHERE ((" . table_friends . ".friend_from)= " . $user_id . " AND ".table_friends.".status = 2) UNION ALL SELECT " . table_users . ".user_login, " . table_users . ".user_names, " . table_users . ".user_avatar_source, " . table_users . ".user_email, " . table_users . ".user_id FROM " . table_friends . " LEFT JOIN " . table_users . " ON (" . table_friends . ".friend_from = " . table_users . ".user_id )  WHERE ((" . table_friends . ".friend_to)= " . $user_id . " AND ".table_friends.".status = 2);",ARRAY_A);
		
		
		
		
		
//		$friends = $db->get_results("SELECT " . table_users . ".user_login, " . table_users . ".user_avatar_source, " . table_users . ".user_email, " . table_users . ".user_id FROM " . table_friends . " INNER JOIN " . table_users . " ON " . table_friends . ".friend_to = " . table_users . ".user_id WHERE (((" . table_friends . ".friend_from)= " . $user_id . "));",ARRAY_A);

		return $friends;

	}
           function get_friend_list_sug_friend1($user_id)
	{
		// returns an array of people you've added as a friend
		global $db, $current_user;
		//echo "SELECT " . table_users . ".user_login FROM " . table_friends . " INNER JOIN " . table_users . " ON friends.friend_to = " . table_users . ".user_id WHERE (((friends.friend_from)=$current_user->user_id));";
	// $friends = $db->get_results("SELECT " . table_users . ".user_login AS user_log, " . table_users . ".user_avatar_source AS user_av, " . table_users . ".user_email AS user_m, " . table_users . ".user_id AS user_d FROM " . table_friends . " AS t_friends LEFT JOIN " . table_users . " AS table_1 ON " . table_friends . ".friend_to = " . table_users . ".user_id  WHERE (((" . table_friends . ".friend_from)= " . $user_id . ")) LEFT JOIN " . table_users . "  AS table_2 ON " . table_friends . ".friend_from = " . table_users . ".user_id  WHERE (((" . table_friends . ".friend_to)= " . $user_id . "));",ARRAY_A);

	        $friends = $db->get_results("SELECT " . table_users . ".user_login, " . table_users . ".user_names, " . table_users . ".user_avatar_source, " . table_users . ".user_email, " . table_users . ".user_id, " . table_friends . ".status FROM " . table_friends . " LEFT JOIN " . table_users . " ON (" . table_friends . ".friend_to = " . table_users . ".user_id )  WHERE ((" . table_friends . ".friend_from)= " . $user_id . " AND ".table_friends.".status = 2) UNION ALL SELECT " . table_users . ".user_login, " . table_users . ".user_names, " . table_users . ".user_avatar_source, " . table_users . ".user_email, " . table_users . ".user_id FROM " . table_friends . " LEFT JOIN " . table_users . " ON (" . table_friends . ".friend_from = " . table_users . ".user_id )  WHERE ((" . table_friends . ".friend_to)= " . $user_id . " AND (".table_friends.".status = 2 OR ".table_friends.".status = 3));",ARRAY_A);

        //$friends = $db->get_results("SELECT " . table_users . ".user_login, " . table_users . ".user_avatar_source, " . table_users . ".user_email, " . table_users . ".user_id FROM " . table_friends . " INNER JOIN " . table_users . " ON " . table_friends . ".friend_to = " . table_users . ".user_id WHERE (((" . table_friends . ".friend_from)= " . $user_id . "));",ARRAY_A);

		return $friends;

	}

	function get_friend_list_2()
	{
		// returns an array of people who have added you as a friend
		global $db, $current_user;
		$friends = $db->get_results("SELECT " . table_users . ".user_login, " . table_users . ".user_avatar_source, " . table_users . ".user_email, " . table_users . ".user_id FROM " . table_friends . " INNER JOIN " . table_users . " ON " . table_friends . ".friend_from = " . table_users . ".user_id WHERE ((" . table_friends . ".friend_to)= " . $current_user->user_id . ")",ARRAY_A);
		return $friends;
	}
	function get_friend_status2($friend)
	{
		global $db, $current_user;
		if (!is_numeric($friend)) die();
			$sql = 	"SELECT recomendado FROM pligg_recomendados WHERE  recomendado = ".$friend." AND recomendadop = " . $current_user->user_id . ";"
;	
//	$sql = "SELECT " . table_users . ".user_id FROM " . table_friends . " INNER JOIN " . table_users . " ON " . table_friends . ".friend_to = " . table_users . ".user_id WHERE " . table_friends . ".friend_from=" . $current_user->user_id . " and " . table_friends . ".friend_to=".$friend.";";
		//echo $sql;
		$friends = $db->get_var($sql);
		//echo $friends;
		return $friends;
		
		// returns friend user_id if a friend
		// returns null if not
	}


	function get_friend_status3($friend)
	{
		global $db, $current_user;
		if (!is_numeric($friend)) die();

		$sql = "SELECT " . table_users . ".user_id FROM " . table_friends . " INNER JOIN " . table_users . " ON " . table_friends . ".friend_to = " . table_users . ".user_id WHERE (" . table_friends . ".friend_from=" . $current_user->user_id . " and " . table_friends . ".friend_to=".$friend." and status = '1') or (". table_friends . ".friend_to=" . $current_user->user_id . " and " . table_friends . ".friend_from=".$friend." and status = '1');";
		//echo $sql;
		$friends = $db->get_var($sql);
		//echo $friends;
		return $friends;		
		// returns friend user_id if a friend
		// returns null if not
	}

       

	function get_friend_status($friend)
	{
		global $db, $current_user;
		if (!is_numeric($friend)) die();

		$sql = "SELECT " . table_users . ".user_id FROM " . table_friends . " INNER JOIN " . table_users . " ON " . table_friends . ".friend_to = " . table_users . ".user_id WHERE (" . table_friends . ".friend_from=" . $current_user->user_id . " and " . table_friends . ".friend_to=".$friend." and (status = '1' or status = '2') ) or (". table_friends . ".friend_to=" . $current_user->user_id . " and " . table_friends . ".friend_from=".$friend."  and (status = '1' or status = '2'));";
		//echo $sql;
		$friends = $db->get_var($sql);
		//echo $friends;
		return $friends;		
		// returns friend user_id if a friend
		// returns null if not
	}
}

?>